import * as turf from '@turf/turf'
import { ITemplatePointInfo, PointType } from '@/types/wayline'

/**
 * 解析坐标字符串
 * @param coordStr 坐标字符串 "Lon,Lat,height"
 * @returns 坐标对象
 */
export const parseCoordinates = (coordStr: string): PointType => {
  const [Lon, Lat, height] = coordStr.split(',').map(Number)
  return { Lon, Lat, height: height || 0 }
}
/**
 * 处理多边形数据
 */
export const processPolygonData = (points: ITemplatePointInfo[]): { polygon: any; coordinates: any } => {
  const polygons: number[] = []

  points.forEach((point) => {
    point.Polygon.outerBoundaryIs.LinearRing.coordinates.split(' ').forEach((coord) => {
      const { Lon, Lat } = parseCoordinates(coord)
      polygons.push(Lon, Lat)
    })
  })

  const coordinates = Array.from({ length: polygons.length / 2 + 1 }, (_, i) =>
    i < polygons.length / 2 ? polygons.slice(i * 2, i * 2 + 2) : polygons.slice(0, 2)
  )
  console.log(coordinates, 'coordinates')
  return { polygon: turf.polygon([coordinates]), coordinates: coordinates.flat(Infinity) }
}
